/**
 * @class Contacts.views.ContactListPanel
 * @extends Ext.Panel
 * The panel containing our contact list.
 */
var loginPanel = new Ext.form.FormPanel({
	frame: true,
	title:'user login',
	waitMsgTarget: true,
	url:'auth.php',
	items: [
        new Ext.form.FieldSet({
            defaultType: 'textfield',
			autoHeight:true,
            items: [{
				label: 'UserID',
				name: 'loginUserID',
            }, {
				xtype: 'passwordfield',
				name:'loginPassword',  
				label:'Password'
            }, {
				xtype: 'selectfield',
				name:'loginDomain',
				options: [{
					text: 'NUSSTU', value: 'nusstu'
				}, {
					text: 'NUSSTF', value: 'nusstf'
				}, {
					text: 'NUSEXT', value: 'nusext'
				}, {
					text: 'GUEST', value: 'guest'
				}],
				label:'Domain'
            },{
				xtype: 'button',
				text: 'Login',
				ui: 'confirm',
				style: 'margin:2%;width:100px;margin-left:auto;margin-right:auto',
				handler: function() {
					loginPanel.submit({ 
						method:'POST', 
						waitTitle:'Connecting', 
						waitMsg:'Sending data...',
	 
				// Functions that fire (success or failure) when the server responds. 
				// The one that executes is determined by the 
				// response that comes from login.asp as seen below. The server would 
				// actually respond with valid JSON, 
				// something like: response.write "{ success: true}" or 
				// response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
				// depending on the logic contained within your server script.
				// If a success occurs, the user is notified with an alert messagebox, 
				// you define as redirect. 
	 
						success:function(form, result){
							console.log('success');
							Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
								if (btn == 'ok'){
									var redirect = '#Contact/index'; 
									window.location = redirect;
								}
							});
						},
	 
				// Failure function, see comment above re: success and failure. 
				// You can see here, if login fails, it throws a messagebox
				// at the user telling him / her as much.  
	 
						failure:function(form, result){ 
							console.log('failed');
							if(result.failureType == 'server'){ 
								obj = Ext.decode(result.response.responseText); 
								Ext.Msg.alert('Login Failed!', obj.errors.reason); 
							}else{ 
								Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + result.response.responseText); 
							} 
							loginPanel.reset(); 
						}
					}); 
					console.log('waiting for response....');
				}
            }],
			dockedItems: [{
				xtype: 'toolbar',
				dock: 'top',
				title: 'NUS IVLE'
			}]
        })
    ]
});